-- Stone
-- Created by huanghr Mar/14/2016
-- 氪石结界

-- 初始化
function initKryptonite(self)
    self.item:removeAllChildren();

    local class = self.gridData.class;
    local buildingInfo = BuildingBonusM.queryBuildingView(class);

    if not buildingInfo then
        error("获取不到建筑查看配置，元素类型：" .. class);
        return;
    end

    local function doEvent()
        DungeonActionM.go("closed_stone", ME.user.dbase:queryTemp("magic_stone", "pos", false));
        DungeonActionM.go("do_building_bonus", self.gridData:getPos());
    end

    local function onClicked(sender, eventType)
        if eventType == ccui.TouchEventType.ended then
            if self.gridData.state == GRID_STATE_DISABLE then
                -- 已经失效，不处理
                return;
            end

            -- 更新建筑访问
            self:updateBuildingVisit();

            -- 格子不可到达
            if not self.gridData:canHit() then
                alert(getLocStr("grid_unreachable"));
                return;
            end

            local pos = self.gridData:getPos();
            local stoneId = self.gridData.id;
            local stoneType = MagicStoneM.query(stoneId, "type");

            local iconName = MagicStoneM.getStoneIcon(stoneId);
            local iconPath = getLevelItemIconPath(iconName);

            -- 打开石柱界面
            self:openBuildingView(doEvent, nil, nil, nil, nil, iconPath);

            -- 音效
            AudioM.playFx("ui_open1");
        end
    end

    local stoneId = self.gridData.id;
    local stoneType = MagicStoneM.query(stoneId, "type");

    -- 用一张透明的图片来接收点击事件
    local iconName = MagicStoneM.getStoneIcon(stoneId);
    local iconPath = getLevelItemIconPath(iconName);
    self.icon = ccui.ImageView:create(iconPath);
    self.icon:setTouchEnabled(true);
    self.icon:addTouchEventListener(onClicked);
    self.item:addChild(self.icon);
    self.icon:setOpacity(0);

    -- 创建石柱模型，初始停在第一帧
    local effectId = iif(stoneType == STONE_TYPE_LIGHT, 1197, 1192);
    local animation = createEffectAnim(effectId);
    animation:gotoFrameAndPause(0);
    self.item.animation = animation;
    self.item:addChild(animation);

    self.item:setPositionY(10);
end

-- 关注石柱关闭的事件
function onCloseStone(self)
    -- 播放石柱关闭特效
    playStoneCloseEffect(self);
end